<template>
  <div style="display: none;">
    <!-- 这是一个无UI的数据初始化组件 -->
  </div>
</template>

<script>
import { onMounted, watch } from 'vue';
import { useAuthStore } from '../store/auth';
import { useSubscriptionStore } from '../store/subscription';

export default {
  name: 'DataInitializer',
  setup() {
    const authStore = useAuthStore();
    const subscriptionStore = useSubscriptionStore();

    // 初始化数据的方法
    const initializeData = async () => {
      if (!authStore.isAuthenticated) {
        return;
      }

      try {
        // 并行加载套餐信息和用户订阅信息
        await Promise.all([
          subscriptionStore.fetchPricingPlans(),
          subscriptionStore.fetchCurrentSubscription()
        ]);
      } catch (error) {
        console.error('初始化数据失败:', error);
      }
    };

    // 监听认证状态变化
    watch(
      () => authStore.isAuthenticated,
      (isAuthenticated) => {
        if (isAuthenticated) {
          initializeData();
        } else {
          // 用户登出时清空订阅数据
          subscriptionStore.$reset();
        }
      },
      { immediate: true }
    );

    // 组件挂载时初始化数据
    onMounted(() => {
      initializeData();
    });

    return {};
  }
};
</script>